package com.zuoqi.easywork.userManage.controller;

import com.github.pagehelper.Page;
import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.PageView;
import com.zuoqi.common.utils.ReturnMapUtils;
import com.zuoqi.common.utils.SessionUtil;
import com.zuoqi.easywork.userManage.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class scheduleController {
    @Autowired
    private ScheduleService scheduleService;


    @RequestMapping(value = "/scheduleCon/ajax/getScheduleList",method = RequestMethod.POST)
    @ResponseBody
    public Map getScheduleList(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userDepart = SessionUtil.getLoginUserDepart(session);
        params.put("depart",userDepart);
        params.put("state","1");
        Page page = PageView.startPageForTable(params, false);
        List<IMap> projectList = scheduleService.getScheduleList(params);
        Map<String, Object> result = PageView.getResultForTable(page, projectList);
        return result;
    }
    @RequestMapping(value = "/scheduleCon/ajax/getMyScheduleList",method = RequestMethod.POST)
    @ResponseBody
    public Map getMyScheduleList(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("userId",userId);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> projectList = scheduleService.getScheduleList(params);
        Map<String, Object> result = PageView.getResultForTable(page, projectList);
        return result;
    }
    /**
     * 提交项目进度汇报
     * */
    @RequestMapping(value = "/scheduleCon/ajax/addSchedule")
    @ResponseBody
    public Map  addSchedule(HttpServletRequest request) {
        IMap param = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        String userName = SessionUtil.getLoginUserName(session);
        param.put("updateUser",userId);
        param.put("userName",userName);
        int ret = scheduleService.addSchedule(param);
        if (1==ret){
            return ReturnMapUtils.businessResult("0000","提交成功！");
        }else {
            return ReturnMapUtils.businessResult("8888","提交失败！");
        }
    }
    /**
     * 提交项目进度汇报
     * */
    @RequestMapping(value = "/scheduleCon/ajax/recallSchedule")
    @ResponseBody
    public Map recallSchedule(HttpServletRequest request) {
        IMap param = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        String userName = SessionUtil.getLoginUserName(session);
        param.put("updateUser",userId);
        int ret = scheduleService.recallSchedule(param);
        if (1==ret){
            return ReturnMapUtils.businessResult("0000","撤销成功！");
        }else if (ret == 0){
            return ReturnMapUtils.businessResult("8888","仅可撤销当日记录！");
        }else {
            return ReturnMapUtils.businessResult("8888","撤销失败！");
        }
    }
}
